package appagendaprofesores.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Assert;

import appagendaprofesores.excepcion.DAOExcepcion;
import appagendaprofesores.modelo.Estudiante;
import appagendaprofesores.negocio.GestionEstudiantes;

/**
 * Servlet implementation class GestionEstudiantesServlet
 */
@WebServlet("/GestionEstudiantesServlet")
public class GestionEstudiantesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GestionEstudiantesServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String Nombre = request.getParameter("nombre");
		String ApPaterno = request.getParameter("ApPaterno");
		String ApMaterno = request.getParameter("ApMaterno");
		String Sexo = request.getParameter("sexo");
		String TipoDocStr = request.getParameter("TipoDoc");
		String NroDoc = request.getParameter("NroDoc");
		String Email = request.getParameter("Email");
		String Celular = request.getParameter("Celular");
		
		int TipoDocInt;
		String SexoLetra;
		
		switch (Sexo) {
        case "Hombre":
        	SexoLetra = "H";
        	break;
        case "Mujer":
        	SexoLetra = "M";
        	break;
        default:
        	SexoLetra = "H";
        }
		
		switch (TipoDocStr) {
        case "DNI":
        	TipoDocInt = 1;
        	break;
        case "CE":
        	TipoDocInt = 2;
        	break;
        case "Pasaporte":
        	TipoDocInt = 3;
        	break;
        default:
        	TipoDocInt = 1;
        }
		
		GestionEstudiantes negocio = new GestionEstudiantes();
		
		try {
			//int idestudiante,String nombre, String apellidopaterno,String apellidomaterno, String sexo, String nrodocumento,String email, String nrocelular, int iddocumento
						Estudiante c = negocio.insertar (Nombre, ApPaterno, ApMaterno, SexoLetra, NroDoc,Email, Celular, TipoDocInt);
						request.setAttribute("Resultado", "El usuario se a registrado con exito");
						RequestDispatcher rd = request.getRequestDispatcher("estudiante_nuevo.jsp");
					    rd.forward(request, response);
						// response.sendRedirect(request.getContextPath()	+ "/estudiante_nuevo.jsp");

					} catch (DAOExcepcion e) {
						RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
						rd.forward(request, response);
					}
		
		
		
	}

}
